﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;

namespace ORM.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            #region
            SqlBulider sqlBuilder = new SqlBulider();
            // 查询语句生成
            List<SqlParameter> QueryParas;
            string QuerySql = sqlBuilder.GetQuery<UserEntity>(u => u.ID > 1, out QueryParas);
            Console.WriteLine(QuerySql);

            // 添加语句生成
            List<SqlParameter> AddParas;
            UserEntity User = new UserEntity()
            {
                Age = 1,
                Name = "Name",
                ID = 1,
                ClassID = 2020
            };
            string addsql = sqlBuilder.GetInsert(User, out AddParas);
            Console.WriteLine(sqlBuilder.GetInsert(User, out AddParas));
            // 编辑语句生成
            UserEntity Eentity = new UserEntity()
            {
                Age = 1,
                Name = "Name",
                ID = 1,
                ClassID = 2020
            };
            List<SqlParameter> EditParas;
            Console.WriteLine(sqlBuilder.GetUpdate(Eentity, out EditParas));

            //辑语句生成
            List<SqlParameter> DeleteParas;
            Console.WriteLine(sqlBuilder.GetDelete<UserEntity>(u=>u.ID==1,out DeleteParas));

            #endregion

           

            string FilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ORM.Test.dll");
            Assembly ase = Assembly.LoadFile(FilePath);
            Type TestType = ase.GetType("ORM.Test.TestMethod");

            MethodInfo method = TestType.GetMethod("Query");
            object MVC_Test = Activator.CreateInstance(TestType);
            method.Invoke(MVC_Test,new object [] { });
        }


        public static void Add(string addsql,IDbConnection con,List<SqlParameter> pars)
        {
            #region 执行
            // 添加
           
            SqlCommand command = new SqlCommand(addsql,(SqlConnection)con);
            command.Parameters.AddRange(pars.ToArray());
            int resualt = command.ExecuteNonQuery();
            command.Dispose();
            #endregion
        }


        public static void Query(string sql,SqlConnection con,List<SqlParameter> parameters)
        {
            SqlCommand command = new SqlCommand(sql,(SqlConnection)con);
            SqlDataReader Read = command.ExecuteReader();
            command.Parameters.AddRange(parameters.ToArray());
            List<UserEntity> ListUser = new List<UserEntity>();
            Type type = typeof(UserEntity);
            while (Read.Read())
            {
                object obj = Activator.CreateInstance(type);

                foreach (PropertyInfo item in type.GetProperties())
                {
                    var val = Read[item.GetColName()];
                    if (item.PropertyType == typeof(Int32))
                    {
                        item.SetValue(obj, val == DBNull.Value ? -1 : val);
                    }
                    else
                    {



                        item.SetValue(obj, val == DBNull.Value ? null : val);
                    }

                }
                ListUser.Add((UserEntity)obj);
            }
        }
    }
}
